
var postCommon = function () {
    checkLogin(function () {
        Prepare({
            complete: function (location) {
                var postTitle = $('.vote-title').val().trim(),
                    images = $('.uploadfuck').find('img').map(function (i, v) {
                        return v.src
                    }).toArray().toString() || '',
                    postContent = $('textarea').val().trim()

                $.ajax({
                    url: baseurl + '/post/publish/notice.do',
                    dataType: 'jsonp',
                    data: {
                        postTitle: postTitle,
                        poiId: location.poiId,
                        images: images,
                        postContent: postContent,
                        startTime: $('#start-time').val() + ' 00:00:00',
                        endTime: $('#over-time').val() + ' 23:59:59'
                    }
                }).then(function (data) {
                    ensureCorrectRes(data, function () {
                        console.log(data);
                        AlertBox({
                            msg: '发布成功',
                            type: 'mini',
                        })
                    })
                })
            }
        })

    })

}

// var checkImage = function () {
//     var all = true
//     var imgs = $('img');
//     for (var i = 0; i < imgs.length; i++) {
//         if (!$(imgs[i]).attr('src')) {
//             $(imgs[i]).hide();
//             all = false;
//         }
//     }
//     if (all) {
//         $('.upload-text').hide();
//     }
//     else {
//         $('.upload-text').show();
//     }
// }

var uploadel;
$(function () {
    getDefaultPoi();
    $('textarea').on('input', function () {
        var text = $(this).val()
        if (text.length) {
            $('.send-post').css('background', '#fa0')
        } else {
            $('.send-post').attr('style', '')
        }

    });
    $('.send-post').click(function () {
        if (!$('textarea').val().trim()) {
            return;
        }
        postCommon()
    });
    //checkImage();
    //  $('#filechooser').on('change', uploadCallback)
    checkLogin();
    if (window.inApp && window.inApp.android) {
        androidUpload2(function (data,target) {
            var ele = $('<li class="uploadfuck"><img></li>');
            uploadel = target
            ele.find('img').attr('src', data).show();
            if ($(uploadel).hasClass('J-big')) {
                var ul = $(uploadel).siblings('ul');
                ul.append(ele);
                var left = parseInt($(uploadel).css('left'), 10);
                var width = parseInt($(uploadel).css('width'), 10);
                $(uploadel).css('left', left + width + 10)
            }
            if (ul.find('.uploadfuck').length == 5) {
                $(uploadel).hide();
            }
        })

    }
    else {
        var domain_name = (env == 'pre' || env == 'sit') ? 'cnsuning.com' : 'suning.com';
        document.domain = window.domain = domain_name;
        $('#filechooser').AjaxFileUpload({
            action: baseurl + '/post/save/fileUpload.do',
            onComplete: uploadCallback
        });
    }

    $('#suck').click(function () {
        $('#start-time').click();
    });
    $('#you').click(function () {
        $('#over-time').click();
    });

    $('.addimg').click(function (e) {
        uploadel = e.target;
        console.log(uploadel);
        $('#filechooser').click();
    })

})

var uploadCallback = function (data) {
    if (!(+data.result)) {
        console.log(uploadel);
        var ele = $('<li class="uploadfuck"><img></li>');
        ele.find('img').attr('src', data.url).show();
        if ($(uploadel).hasClass('J-big')) {
            var ul = $(uploadel).siblings('ul');
            ul.append(ele);
            var left = parseInt($(uploadel).css('left'), 10);
            var width = parseInt($(uploadel).css('width'), 10);
            $(uploadel).css('left', left + width + 5)
        }
        if (ul.find('.uploadfuck').length == 5) {
            $(uploadel).hide();
        }
    }
    else {
        AlertBox({
            type: 'mini',
            msg: '上传失败'
        })
    }
}


//starMobiscroll配置
var starMobiscroll = $('#start-time').mobiscroll().date({
    lang: "zh",
    setText: '确定',
    cancelText: '取消',
    dayText: '日',
    monthText: '月',
    yearText: '年',
    dateFormat: 'yy-mm-dd',// 日期格式
    endYear: 2050,
    min: new Date(),
    onCancel: function (event, inst) {
        //取消输入，清空input，并禁用结束时间选择
        $('#start-time,#over-time').val('');
        $('#start-time').attr('placeholder', '起始时间');
        $('#over-time').mobiscroll('option', {
            disabled: true
        });
        $('.admin-post').addClass('disable');
    },
    onSet: function (event, inst) {
        //选定开始时间
        var startDate = $('#start-time').mobiscroll('getVal');
        var overDate = $('#over-time').mobiscroll('getVal');
        if (overDate < startDate) {
            //判断开始时间不得大于结束时间
            $('#over-time').val('');
            $('.admin-post').addClass('disable');
        }
        //修改结束时间参数
        $('#over-time').mobiscroll('option', {
            min: startDate,
            disabled: false
        });
        console.log('ss');
        $('#suck').hide()

    }
});
var overMobiscroll = $('#over-time').mobiscroll().date({
    lang: "zh",
    setText: '确定',
    cancelText: '取消',
    dayText: '日',
    monthText: '月',
    yearText: '年',
    dateFormat: 'yy-mm-dd',// 日期格式
    endYear: 2050,
    disabled: true,
    onCancel: function (event, inst) {
        $('#over-time').val('');
        $('.admin-post').addClass('disable');
    },
    onSet: function (event, inst) {
        $('#you').hide();
        console.log('sss');

    }
});


$('.choose-dis').click(function () {
    location.href = '../choose-district.html';
})
//获取默认小区
var getDefaultPoi = function () {
    Prepare({
        complete: function (location) {
            console.log(location);
            var poiId = location.poiId;
            var poiName = location.poiName;
            $('.choose-dis span').html('（' + poiName + '）');
            $('.choose-dis span').attr('poiId', poiId);
        }
    })
}